/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  edb_domain.h
 *  scatterd
 *
 *  Created by Grant Jones on 3/1/08.
 *
 */

#ifndef EDB_DOMAIN_H
#define EDB_DOMAIN_H

#include "radixtree.h"
#include "radixtree_enum.h"

#define EDB_SYNC_DOMAIN	"edbsync"

typedef struct
{
	int refcount;
	
	rtree_t *rt_db_ptr;
	strmapStruct index;

	size_t dname_sz;
	/* dname MUST be at the end of this structure: */
	char dname[1];
} edb_domain_t;


typedef struct
{
	mempool_t *pool;	/* where this structure was allocated from */
	
	/* the domain reference */
	edb_domain_t *d;
	
	/* a copy of the pcs we need for doing sync with other dbs */
	ProgramContextStruct *pcs;
	
	/* current transaction id */
	int tid;
	
} edb_domain_ref_t;


edb_domain_ref_t *edb_domain_use( ProgramContextStruct *pcs, mempool_t *pool, const char *dname, int dname_len );
int edb_domain_release( ProgramContextStruct *pcs, edb_domain_ref_t *dref );

/*!
	@function edb_domain_notify_sync
	@abstract inserts a notification that an item in a domain needs to be synced
	@discussion this is called internally by insert and delete calls right after they
				make a change to an item, the domain name and item name are recorded
	@param dref domain reference; is checked to make sure it isn't the sync domain
	@param changed_key the item key name (or "_name" value)
	@result no return value; the affected key and domain are inserted into the sync
			domain
*/
void edb_domain_notify_sync( edb_domain_ref_t *dref, rtvalue_t *changed_key );

int edb_domain_begin( edb_domain_ref_t *dref );
int edb_domain_end( edb_domain_ref_t *dref );

int edb_domain_construct_item( edb_domain_ref_t *dref, rtvalue_t *data_out, AttrList *insert_values );
int edb_domain_destruct_item( mempool_t *pool, edb_domain_ref_t *dref, rtvalue_t *data_in, AttrList **attrs_out );
#endif


